package org.example.mpdemo1.controller;

import org.apache.ibatis.annotations.Delete;
import org.example.mpdemo1.entity.InstruRes;
import org.example.mpdemo1.entity.Reservation;
import org.example.mpdemo1.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Controller
@CrossOrigin
public class ReservationController {

    @Autowired
    private ReservationService reservationService;

    @GetMapping("/reservation/instrument/{instrumentId}")
    @ResponseBody
    public List<Reservation> getReservationByInstrumentId(@PathVariable("instrumentId") int instrumentId) {
        return reservationService.getReservationByInstrumentId(instrumentId);
    }

    @GetMapping("/reservation/user/{userId}")
    @ResponseBody
    public List<Reservation> getReservationByUserId(@PathVariable("userId") int userId) {
        return reservationService.getReservationByUserId(userId);
    }

    @GetMapping("/reservation/getall")
    @ResponseBody
    public List<Reservation> getAllReservation() {
        return reservationService.getAllReservation();
    }

    @PostMapping("/reservation")
    @ResponseBody
    public String addReservation(Reservation reservation) {
        reservation.setCreateTime(new Timestamp(System.currentTimeMillis()));
        reservation.setStatus(0);
        Map<String, Object> map =  reservationService.addReservation(reservation);
        if (Objects.isNull(map) || map.isEmpty()) {
            return "success";
        } else {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                System.out.println(entry.getKey() + ":" + entry.getValue());
            }
            return map.toString();
        }
    }

    @GetMapping("/reservation/getAllInstrument")
    @ResponseBody
    public List<InstruRes> getAllInstrument() {
        return reservationService.getAllInstruRes();
    }

    @PostMapping("/reservation/updateStatus")
    @ResponseBody
    public String updateStatus(int id, int status) {
        reservationService.updateStatus(id, status);
        return "success";
    }

    @DeleteMapping("/reservation/{id}")
    @ResponseBody
    public String deleteReservation(@PathVariable("id") int id) {
        reservationService.deleteReservation(id);
        return "success";
    }
}
